Longest substring without repeating characters

       
        
        Given a string S, find the length of the longest substring without repeating characters.

        Example 1:

        Input:
        S = "geeksforgeeks"
        Output:
        7
        Explanation:
        Longest substring is "eksforg".

        
        
Code #include<bits/stdc++.h> using namespace std; int main() { int i,j,k,n; string s; cin>>s; n=s.size(); unordered_map<int,int>m; int mx=INT_MIN; i=0,j=0; while(j<n) { m[s[j]]++; if(m.size()==j-i+1) { mx=max(mx,j-i+1); j++; } else if(m.size()<j-i+1) { while(m.size()<j-i+1) { m[s[i]]--; if(m[s[i]]==0) m.erase(s[i]); i++; } j++; } } cout<<mx<<endl; return 0; }